﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class RatingRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public RatingRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertRating(tSite_Photos_Rating rating)
        {
            context.tSite_Photos_Ratings.InsertOnSubmit(rating);
        }

        public List<tSite_Photos_Rating> GetRatesByImageId(int ImageId)
        {
            List<tSite_Photos_Rating> query = (from n in context.tSite_Photos_Ratings
                                               where n.IID == ImageId
                                               select n).ToList();
            return query;
        }

        public bool IsAlreadyRate(int ImageID, Guid UserID)
        {
            int query = (from n in context.tSite_Photos_Ratings
                         where n.IID == ImageID && n.UID == UserID
                         select n).Count();
            return (query > 0);
        }

        public void DeleteUserRatings(Guid UID)
        {
            List<tSite_Photos_Rating> lRates = (from r in context.tSite_Photos_Ratings
                                                where r.UID == UID
                                                select r).ToList();

            if (lRates != null && lRates.Count > 0)
            {
                context.tSite_Photos_Ratings.DeleteAllOnSubmit(lRates);
            }
        }

        public void DeletePictureRatingsByAlbumID(int AID)
        {
            List<tSite_Photos_Rating> lRates = (from r in context.tSite_Photos_Ratings
                                                join i in context.tSite_Photos_Images on r.IID equals i.ID
                                                where i.AID == AID
                                                select r).ToList();

            if (lRates != null && lRates.Count > 0)
            {
                context.tSite_Photos_Ratings.DeleteAllOnSubmit(lRates);
            }
        }

        public void DeletePictureRatingsByImageID(int IID)
        {
            List<tSite_Photos_Rating> lRates = (from r in context.tSite_Photos_Ratings
                                                where r.IID == IID
                                                select r).ToList();

            if (lRates != null && lRates.Count > 0)
            {
                context.tSite_Photos_Ratings.DeleteAllOnSubmit(lRates);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
}
